package bb.lanxing.mvp.presetner;

import java.util.Collections;
import java.util.List;

import bb.lanxing.model.LevelDataItem;
import bb.lanxing.mvp.model.AreaLevelDataModelImpl;
import bb.lanxing.mvp.model.i.LevelDataModel;
import bb.lanxing.mvp.presetner.i.AreaSelectionPresenter;
import bb.lanxing.mvp.view.i.AreaSelectionView;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;

public class AreaSelectionPresenterImpl implements AreaSelectionPresenter {
    private AreaSelectionView areaSelectionView;
    private LevelDataModel levelDataModel = new AreaLevelDataModelImpl();

    @Override
    public void destroy() {
    }

    public AreaSelectionPresenterImpl(AreaSelectionView areaSelectionView) {
        this.areaSelectionView = areaSelectionView;
    }

    @Override
    public void requestLevelData() {
        this.levelDataModel.requestLevelData(null, null).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                if (AreaSelectionPresenterImpl.this.areaSelectionView != null) {
                    AreaSelectionPresenterImpl.this.areaSelectionView.onData(Collections.emptyList());
                }
            }

            @Override
            public void onNext(List<LevelDataItem> levelDataItem) {
                if (AreaSelectionPresenterImpl.this.areaSelectionView != null) {
                    AreaSelectionPresenterImpl.this.areaSelectionView.onData(levelDataItem);
                }
            }
        });
    }
}
